"""
工单管理数据模型
对应数据库表: wo_work_order, wo_work_order_log
用于数据库操作和业务逻辑处理
"""
from datetime import datetime
from typing import Optional, Any

from pydantic import BaseModel, Field, ConfigDict

from common.base_enums import (
	WorkOrderStatus, WorkOrderPriority, WorkOrderSourceType,
	WorkOrderDeviceType, WorkOrderAction
)


class WorkOrder(BaseModel):
	"""工单主表模型"""
	id: int = Field(..., description="主键ID")
	code: str = Field(..., description="工单编码 W{yyyyMMdd}{4位流水}")
	title: str = Field(..., description="标题")
	source_type: WorkOrderSourceType = Field(..., description="来源类型")
	alarm_id: Optional[int] = Field(None, description="关联告警ID")
	device_type: Optional[WorkOrderDeviceType] = Field(None, description="目标设备类型")
	device_id: Optional[int] = Field(None, description="目标设备ID")
	priority: WorkOrderPriority = Field(WorkOrderPriority.MEDIUM, description="优先级")
	description: Optional[str] = Field(None, description="问题描述")
	expect_finish_time: Optional[datetime] = Field(None, description="期望完成时间")
	assignee_code: Optional[str] = Field(None, description="负责人编码")
	status: WorkOrderStatus = Field(WorkOrderStatus.CREATED, description="状态")
	solution: Optional[str] = Field(None, description="解决方案")
	close_comment: Optional[str] = Field(None, description="关闭说明")
	closed_by: Optional[str] = Field(None, description="关闭人")
	closed_at: Optional[datetime] = Field(None, description="关闭时间")
	ext: Optional[Any] = Field(None, description="扩展字段")
	deleted: int = Field(0, description="是否删除")
	created_at: datetime = Field(..., description="创建时间")
	updated_at: datetime = Field(..., description="更新时间")

	model_config = ConfigDict(from_attributes=True)


class WorkOrderLog(BaseModel):
	"""工单流转日志模型"""
	id: int = Field(..., description="主键ID")
	wo_id: int = Field(..., description="工单ID")
	action: WorkOrderAction = Field(..., description="操作类型")
	actor_code: str = Field(..., description="操作者编码")
	actor_name: Optional[str] = Field(None, description="操作者姓名")
	comment: Optional[str] = Field(None, description="备注")
	old_status: Optional[WorkOrderStatus] = Field(None, description="原状态")
	new_status: Optional[WorkOrderStatus] = Field(None, description="新状态")
	created_at: datetime = Field(..., description="创建时间")

	model_config = ConfigDict(from_attributes=True)
